import tkinter
import numpy
import math


def get_items(get_list):
	tmp_list = []
	for i in range(len(get_list)):
		tmp_list += list(get_list[i])

	tmp_list = numpy.unique(tmp_list)
	return tmp_list


def calculate_TF(str_to_cal,list2):
	denominator = len(str_to_cal)
	ret_list = [0] * len(list2)
	for i in range(len(list2)):
		ret_list[i] = str_to_cal.count(list2[i])/denominator

	return ret_list

def calculate_IDF(to_one_list,all_list):
	ret_list = [0] * len(all_list)
	tmp_list = [0] * len(all_list)
	molecule = len(to_one_list)
	for i in range(len(all_list)):
		for j in range(molecule):
			if all_list[i] in to_one_list[j]:
				tmp_list[i] += 1
	# print(tmp_list)
	for i in range(len(all_list)):
		ret_list[i] = math.log10(molecule/tmp_list[i])

	return ret_list


def calculate_TF_IDF(TF,IDF):
	ret_list = [0] * len(TF)
	for i in range(len(TF)):
		ret_list[i] = TF[i] * IDF[i]
	return ret_list
	

def add_header(list1,all_list):
	ret_str = ""
	for i in range(len(list1)):
		ret_str = ret_str + all_list[i] + ":" + str(list1[i]) + '\n'
	return ret_str


def main():
	global entry_string1,entry_string2
	X = en1.get()
	Y =en2.get()
	to_one_list = [X,Y]
	all_list = get_items(to_one_list) #将单独的所有元素取出来
	print(len(all_list))

	X_toTF = calculate_TF(X,all_list)
	Y_toTF = calculate_TF(Y,all_list)

	IDF = calculate_IDF(to_one_list,all_list)

	X_TF_IDF = calculate_TF_IDF(X_toTF,IDF)
	Y_TF_IDF = calculate_TF_IDF(Y_toTF,IDF)

	#print(X_toTF)
	#print(Y_toTF)
	#print(IDF)
	print(max(X_TF_IDF))
	#print(Y_TF_IDF)

	en3.insert('1.0',add_header(X_toTF,all_list))
	en4.insert('1.0',add_header(Y_toTF,all_list))
	en5.insert('1.0',add_header(IDF,all_list))
	en6.insert('1.0',add_header(X_TF_IDF,all_list))
	en7.insert('1.0',add_header(Y_TF_IDF,all_list))


top = tkinter.Tk()

top.title('TF.IDF计算实验')
top.geometry('500x300')

lab1 = tkinter.Label(top, text = "s1")
lab1.place(x = 10, y = 5)
en1 = tkinter.Entry(top, width = 35)
en1.place(x = 40, y = 5)

lab2 = tkinter.Label(top, text = "s2")
lab2.place(x = 10, y = 35)
en2 = tkinter.Entry(top, width = 35)
en2.place(x = 40, y = 35)

lab3 = tkinter.Label(top, text = "s1 TF")
lab3.place(x = 10, y = 85)
en3 = tkinter.Text(top, bd=1, font=('等线', 10), width=18, height=5)
en3.place(x = 10, y = 110)

lab4 = tkinter.Label(top, text = "s2 TF")
lab4.place(x = 150, y = 85)
en4 = tkinter.Text(top, bd=1, font=('等线', 10), width=18, height=5)
en4.place(x = 150, y = 110)

lab5 = tkinter.Label(top, text = "IDF")
lab5.place(x = 290, y = 85)
en5 = tkinter.Text(top, bd=1, font=('等线', 10), width=18, height=5)
en5.place(x = 290, y = 110)

lab6 = tkinter.Label(top, text = "s1 TF.IDF")
lab6.place(x = 10, y = 175)
en6 = tkinter.Text(top, bd=1, font=('等线', 10), width=18, height=5)
en6.place(x = 10, y = 210)

lab7 = tkinter.Label(top, text = "s2 TF.IDF")
lab7.place(x = 150, y = 175)
en7 = tkinter.Text(top, bd=1, font=('等线', 10), width=18, height=5)
en7.place(x = 150, y = 210)

#按钮
b = tkinter.Button(top, text = "计算", width = 8, command = main)
b.place(x = 300, y = 30)

top.mainloop()
